//
//  GesturePasswordVC.m
//  MRBang
//
//  Created by 铅笔 on 16/4/28.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "GesturePasswordVC.h"
#import "DrawGesturePasswordVC.h"//绘制手势密码 界面

@interface GesturePasswordVC ()<UITableViewDataSource,UITableViewDelegate>
{

    UIButton *button_startGesture;
    BOOL isOpenGestureLock;//是否开始手势密码锁
    
    NSMutableArray *array_title;

}
@property (nonatomic,strong) UITableView *tableView_setPwd;
@end

@implementation GesturePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手势密码设置";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    array_title = [@[@"开启密码锁定",@"重置手势密码"] mutableCopy];

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isOpenGestureLock"]) {
        
        isOpenGestureLock = YES;
    }
    else
    {
        isOpenGestureLock = NO;
    }
    
    [self initWithGesturePasswordVCSubviews];
}


#pragma mark --- 创建视图界面
- (void) initWithGesturePasswordVCSubviews
{
    self.tableView_setPwd = [[UITableView alloc] init];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isOpenGestureLock"]) {
        
        self.tableView_setPwd.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-93*PROPORTION_WIDTH-20);
    }
    else
    {
        self.tableView_setPwd.frame = CGRectMake(0, 0, kScreenWidth, 108);
    }

    [self.view addSubview:self.tableView_setPwd];
    self.tableView_setPwd.dataSource = self;
    self.tableView_setPwd.delegate = self;
    self.tableView_setPwd.showsVerticalScrollIndicator = NO;
    self.tableView_setPwd.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView_setPwd.backgroundColor = COLOR_EVERYVIEWBG;
    self.tableView_setPwd.scrollEnabled = NO;
}

#pragma  mark ----UITableViewDataSource

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array_title.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellFillInInformationID = @"cellFillInInformationID";
    
    UITableViewCell *cell_fillInfo = [tableView cellForRowAtIndexPath:indexPath];
    if (cell_fillInfo==nil) {
        cell_fillInfo = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellFillInInformationID];
    }
    
    UIView *view_line = [[UIView alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, cell_fillInfo.height-1, kScreenWidth, 1.0)];
    [cell_fillInfo.contentView addSubview:view_line];
    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#EEEFF1"];
    
    cell_fillInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *label_avatar = [[UILabel alloc] initWithFrame:CGRectMake(47*PROPORTION_WIDTH, 36*PROPORTION_WIDTH, 156*PROPORTION_WIDTH, 34*PROPORTION_WIDTH)];
    [cell_fillInfo.contentView addSubview:label_avatar];
    label_avatar.text = array_title[indexPath.row];
    label_avatar.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
    
    if (indexPath.row==0) {
        
        //开启 手势 与否
        button_startGesture = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-113*PROPORTION_WIDTH, 33*PROPORTION_WIDTH, 83*PROPORTION_WIDTH, 35*PROPORTION_WIDTH)];
        [cell_fillInfo.contentView addSubview:button_startGesture];
        
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isOpenGestureLock"]) {
             [button_startGesture setBackgroundImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
            
        }
        else
        {
             [button_startGesture setBackgroundImage:[UIImage imageNamed:@"unsend"] forState:UIControlStateNormal];
        }
       
        [button_startGesture addTarget:self action:@selector(useStartGesturePwdYesOrNoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell_fillInfo;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==1) {
        DrawGesturePasswordVC *drawGestureView = [[DrawGesturePasswordVC alloc] init];
        [self.navigationController pushViewController:drawGestureView animated:NO];
    }
}

#pragma mark -- 用户开启密码锁 与否
- (void) useStartGesturePwdYesOrNoAction
{
    if (isOpenGestureLock==NO) {
        [button_startGesture setBackgroundImage:[UIImage imageNamed:@"send"] forState:UIControlStateNormal];
        isOpenGestureLock = YES;
        
        self.tableView_setPwd.frame =  CGRectMake(0, 0, kScreenWidth, kScreenHeight-93*PROPORTION_WIDTH-20);
        
        [[NSUserDefaults standardUserDefaults] setObject:@"setGestureLock" forKey:@"isOpenGestureLock"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        return;
    }
    
    if (isOpenGestureLock==YES) {
        [button_startGesture setBackgroundImage:[UIImage imageNamed:@"unsend"] forState:UIControlStateNormal];
        isOpenGestureLock = NO;
        self.tableView_setPwd.frame =  CGRectMake(0, 0, kScreenWidth, 108);
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"isOpenGestureLock"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        //移除手势
//        [DBGuestureLock clearGuestureLockPassword];
        return;
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
